package controller.developer;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import core.modules.base.entity.SysMenu;
import core.modules.base.service.SysMenuService;
import core.modules.developer.entity.Generator;
import core.modules.developer.entity.Table;
import core.util.AjaxResult;
import core.util.WebUtil;

@Controller
@RequestMapping("developer/generator")
public class GeneratorController {
	Logger LOGGER = LoggerFactory.getLogger(GeneratorController.class);

	@Autowired
	SqlSessionFactory sqlSessionFactory;
	@Autowired
	FreeMarkerConfigurer freeMarkerConfigurer;
	@Autowired
	SysMenuService sysMenuService;
	
	@RequestMapping("list.htm")
	@RequiresPermissions("developer:generator:list")
	public Object index(HttpServletRequest request, HttpServletResponse response, ModelMap map)
			throws Exception {
		String tableName = WebUtil.getStr("tableName", request);
		List<Table> list = Generator.getTablesInfo(sqlSessionFactory.openSession(), tableName);
		map.put("list", list);
		map.put("tableName", tableName);
		return "developer/generator/table";
	}

	@RequestMapping("selectParentMenu.htm")
	public Object selectParentMenu(HttpServletRequest request, HttpServletResponse response,
			ModelMap map) {
		List<SysMenu> menuList = sysMenuService.findList(new SysMenu());
		map.put("list", menuList);
		return "developer/generator/selectParentMenu";
	}
	
	@RequestMapping("saveMenu.json")
	@ResponseBody
	public AjaxResult saveMenu(String module,String tableName,SysMenu condition, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			SysMenu parentMenu = sysMenuService.get(condition.getPid());
			condition.setLevel(parentMenu.getLevel()+1);
			condition.setPids(parentMenu.getPids()+","+parentMenu.getId());
			condition.setPermission(module+":"+condition.getPermission());
			condition.setUrl("/"+module+condition.getUrl());
			sysMenuService.save(condition);
			result = new AjaxResult();
			
			SysMenu commonChild=new SysMenu();
			commonChild.setPid(condition.getId());
			commonChild.setPids(condition.getPids()+","+condition.getId());
			commonChild.setShowflag("0");
			commonChild.setLevel(condition.getLevel()+1);
			commonChild.setPermission(module+":"+tableName+":");
			
			SysMenu viewMenu = new SysMenu();
			WebUtil.copyProperties(commonChild, viewMenu);
			viewMenu.setName(condition.getName()+"-查看");
			viewMenu.setPermission(viewMenu.getPermission()+"view");
			viewMenu.setSort(10);
			sysMenuService.save(viewMenu);
			
			SysMenu addMenu = new SysMenu();
			WebUtil.copyProperties(commonChild, addMenu);
			addMenu.setName(condition.getName()+"-新增");
			addMenu.setPermission(addMenu.getPermission()+"add");
			addMenu.setSort(20);
			sysMenuService.save(addMenu);
			
			SysMenu editMenu = new SysMenu();
			WebUtil.copyProperties(commonChild, editMenu);
			editMenu.setName(condition.getName()+"-编辑");
			editMenu.setPermission(editMenu.getPermission()+"edit");
			editMenu.setSort(30);
			sysMenuService.save(editMenu);
			
			SysMenu delMenu = new SysMenu();
			WebUtil.copyProperties(commonChild, delMenu);
			delMenu.setName(condition.getName()+"-删除");
			delMenu.setPermission(delMenu.getPermission()+"view");
			delMenu.setSort(40);
			sysMenuService.save(delMenu);
		} catch (Exception e) {
			result = new AjaxResult(false, "保存失败:" + e.getMessage());
		}
		return result;
	}
	
	
	
	@RequestMapping("generate.json")
	@ResponseBody
	public AjaxResult generate(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		try {
			String name = WebUtil.getStr("name", request);
			List<Table> list = Generator.getTablesInfo(sqlSessionFactory.openSession(), name);
			for (Table table : list) {
				if (name.equals(table.getName())) {
					Generator.generate(freeMarkerConfigurer.getConfiguration(), table,
							WebUtil.getProperties("generate.path"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxResult();
	}

	@RequestMapping("form.htm")
	@RequiresPermissions("developer:generator:form")
	public String form(String id,HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		try {
			List<Table> list = Generator.getTablesInfo(sqlSessionFactory.openSession(), id);
			if (WebUtil.isNotBlank(list)) {
				map.put("table", list.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "developer/generator/form";
	}
	
	@RequestMapping("menu.htm")
	public String menu(String id,HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		try {
			List<Table> list = Generator.getTablesInfo(sqlSessionFactory.openSession(), id);
			if (WebUtil.isNotBlank(list)) {
				map.put("table", list.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "developer/generator/menu";
	}
	
	@RequestMapping("sql.htm")
	public String sql(String id,HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		try {
			List<Table> list = Generator.getTablesInfo(sqlSessionFactory.openSession(), id);
			if (WebUtil.isNotBlank(list)) {
				map.put("table", list.get(0));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "developer/generator/sql";
	}
	
	
	@RequestMapping("save.json")
	@ResponseBody
	@RequiresPermissions("developer:generator:save")
	public AjaxResult save(Table table,HttpServletRequest request, HttpServletResponse response) {
		try {
			//List<Table> list = Generator.getTablesInfo(sqlSessionFactory.openSession(), table.getName());
			//Table rawTable = list.get(0);
			Generator.generate(freeMarkerConfigurer.getConfiguration(), table,
					WebUtil.getProperties("generate.path"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new AjaxResult(false);
	}
}
